﻿using AutoMapper;
using IntelligentAgriculture.Contracts.Interface;
using IntelligentAgriculture.Domain.Interface.SheepFileModelInterface;
using IntelligentAgriculture.Domain.Interface;
using IntelligentAgriculture.Domain;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IntelligentAgriculture.Contracts.Dto.Input;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Domain.Interface.Iridgepletype;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface.IColumnManagement;

namespace IntelligentAgriculture.Services
{
    public class BodyMeasureModelService:IBodyMeasureModelService
    {

        //体尺仓储
        private readonly IBodyMeasureModelRepository bodyMeasureRe;
        private readonly ILogger<BodyMeasureModelService> logger;
        private readonly IMapper imapper;
        //档案仓储
        private readonly ISheepFileModelRepository sheepModelRe;
        //栋舍仓储
        private readonly IridegRepository ridegRe;
        //栏位仓储
        private readonly IColumnManRepository columnRe;

        public BodyMeasureModelService(IBodyMeasureModelRepository bodyMeasureRe, ILogger<BodyMeasureModelService> logger, IMapper imapper,  ISheepFileModelRepository sheepModelRe, IridegRepository ridegRe, IColumnManRepository columnRe)
        {
            this.bodyMeasureRe = bodyMeasureRe;
            this.logger = logger;
            this.imapper = imapper;
            this.sheepModelRe = sheepModelRe;
            this.ridegRe = ridegRe;
            this.columnRe = columnRe;
        }

       
        /// <summary>
        /// 体尺列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageOutput<BodyMeasureModelOutPut>> BodyMeasureModelList(BodyMeasureModelInPut input)
        {

            var listquery = (from x in await bodyMeasureRe.Bodymeasure()
                             join y in await sheepModelRe.GetSheepfilemodels()
                             on x.EarTag equals y.EarTag
                             join z in ridegRe.Showinfo()
                              on x.RidgepoleId equals z.RidgepoleId
                             join a in columnRe.GetShow()
                             on x.ColumnManagementId equals a.ColumnManagementId
                             where x.Isdelete == 0
                             select new BodyMeasureModelOutPut
                             {
                                 BodyMeasureId = x.BodyMeasureId,
                                 EarTag = x.EarTag,
                                 Variety = y.Variety,
                                 SheepRescissionSex = x.SheepRescissionSex,
                                 RidgepoleId = z.RidgepoleId,
                                 ColumnManagementId = a.ColumnManagementId,
                                 MeasureDate = x.MeasureDate,
                                 MeasureAge = x.MeasureAge,
                                 Height = x.Height,
                                 BodyLength = x.BodyLength,
                                 Chest = x.Chest,
                                 Tube = x.Tube,
                                 Oschea = x.Oschea,
                                 Backfat = x.Backfat,
                                 EyeMuscleArea = x.EyeMuscleArea,
                                 EyeMuscleWidth = x.EyeMuscleWidth,
                                 CreateName = x.CreateName,
                                 CreateTime = x.CreateTime,
                                 UpdateName = x.UpdateName,
                                 UpdateTime = x.UpdateTime,
                                 Isdelete = x.Isdelete,

                             });
            if (!string.IsNullOrEmpty(input.earTag))
            {
                listquery = listquery.Where(x => x.EarTag.Contains(input.earTag));
            }
            if (input.variety > 0)
            {
                listquery = listquery.Where(x => x.Variety == input.variety);
            }
            if (input.sheepRescissionSex != null)
            {
                listquery = listquery.Where(x => x.SheepRescissionSex == input.sheepRescissionSex);
            }

            var result = new PageOutput<BodyMeasureModelOutPut>
            {
                TotalCount = listquery.Count(),
                PageSize = input.PageSize,
            };

            listquery = listquery.OrderBy(c => c.BodyMeasureId).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
            result.Data = imapper.Map<List<BodyMeasureModelOutPut>>(listquery.ToList());
            return result;

        }

        /// <summary>
        /// 删除体尺
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> Delete(params int[] ids)
        {

            return await bodyMeasureRe.Delete(ids);
        }


        /// <summary>
        /// 新增体尺
        /// </summary>
        /// <param name="bodymeasuremodel"></param>
        /// <returns></returns>
        public async Task<int> Add(Bodymeasuremodel bodymeasuremodel)
        {
            var entity = imapper.Map<Bodymeasuremodel>(bodymeasuremodel);
            return await bodyMeasureRe.Add(entity);
        }


        /// <summary>
        /// 更新体尺
        /// </summary>
        /// <param name="bodymeasuremodel"></param>
        /// <returns></returns>
        public async Task<int> Update(Bodymeasuremodel bodymeasuremodel)
        {
            var entity = imapper.Map<Bodymeasuremodel>(bodymeasuremodel);
            return await bodyMeasureRe.Update(entity);
        }




    }




}
